﻿
using Xacml.Core;
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;
using Xacml.Core.Runtime.Functions;
using System;

namespace Xacml.Core.Runtime.DataTypes
{
  public class AnyUri : IDataType
  {
    public IFunction EqualFunction
    {
      get
      {
        return (IFunction) new AnyUriEqual();
      }
    }

    public IFunction IsInFunction
    {
      get
      {
        return (IFunction) new AnyUriIsIn();
      }
    }

    public IFunction SubsetFunction
    {
      get
      {
        return (IFunction) new AnyUriSubset();
      }
    }

    public string DataTypeName
    {
      get
      {
        return "http://www.w3.org/2001/XMLSchema#anyURI";
      }
    }

    internal AnyUri()
    {
    }

    public static bool IsDescendantOf(Uri parent, Uri descendant)
    {
      if (!(parent.Scheme == "urn") || !(descendant.Scheme == "urn"))
        throw new EvaluationException(Resource.ResourceManager[Resource.MessageKey.exc_invalid_uri_schema]);
      string[] strArray1 = parent.AbsolutePath.Split(':');
      string[] strArray2 = descendant.AbsolutePath.Split(':');
      if (strArray1.Length > strArray2.Length)
        return false;
      for (int index = 0; index < strArray1.Length; ++index)
      {
        if (strArray2[index] != strArray1[index])
          return false;
      }
      return true;
    }

    public static bool IsChildrenOf(Uri parent, Uri children)
    {
      if (!(parent.Scheme == "urn") || !(children.Scheme == "urn"))
        throw new EvaluationException(Resource.ResourceManager[Resource.MessageKey.exc_invalid_uri_schema]);
      string[] strArray1 = parent.AbsolutePath.Split(':');
      string[] strArray2 = children.AbsolutePath.Split(':');
      if (strArray1.Length > strArray2.Length || strArray1.Length + 1 != strArray2.Length)
        return false;
      for (int index = 0; index < strArray1.Length; ++index)
      {
        if (strArray2[index] != strArray1[index])
          return false;
      }
      return true;
    }
  }
}
